package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ghost.GhostUserData;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ghost.GhostModule;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.server.pb.GhostPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_GHOST_BUY_ENERGY, desc = "鬼市购买体力")
public class GhostBuyEnergyCmd extends AbstractCommand {

    private int times;

    private GhostUserData userData = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.GhostBuyEnergyReqMsg reqMsg = GhostActivityProto.GhostBuyEnergyReqMsg.parseFrom(packet.getBytes());
        times = reqMsg.getTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!GhostMgr.inOpenTime()) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostMgr.checkUserData(player);

        GhostModule ghostModule = player.getModule(GhostModule.class);
        userData = ghostModule.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_UNLOCK;
            return;
        }
        ret = ghostModule.buyEnergy(times);
    }

    @Override
    public void sendData(GamePlayer player) {
        GhostActivityProto.GhostBuyEnergyRespMsg.Builder builder = GhostActivityProto.GhostBuyEnergyRespMsg.newBuilder();
        builder.setRet(ret);
        if (userData != null) {
            builder.setUserData(GhostPb.parseGhostUserDataMsg(userData));
        }
        player.sendPacket(ClientProtocol.U_GHOST_BUY_ENERGY, builder);
    }
}
